package view
{
	import flash.events.MouseEvent;
	
	import mx.controls.Alert;
	import mx.rpc.events.ResultEvent;
	import mx.utils.StringUtil;
	
	import org.puremvc.as3.interfaces.IMediator;
	import org.puremvc.as3.interfaces.INotification;
	import org.puremvc.as3.patterns.mediator.Mediator;
	
	import services.mainservice.MainService;
	
	import valueObjects.Contact;
	
	public class ContactMediator extends Mediator implements IMediator
	{
		public static const CONTACT_MEDIATOR:String = "CONTACT_MEDIATOR";
		
		private var type:int;
		private var contact:Contact;
		
		public function ContactMediator(viewComponent:Object=null,type:int=0,contact:Contact=null)
		{
			super(CONTACT_MEDIATOR, viewComponent);
			this.type = this.contactView.type = type;
			this.contact = this.contactView.con =  contact;
			this.contactView.create.addEventListener(MouseEvent.CLICK,onClick);
		}
		
		private function onClick(evt:MouseEvent):void {
			switch(type) {
				case 0:
					onCreate(evt);
					break;
				case 1:
					onEdit(evt);
					break;
				case 2:
					break;
			}
		}
		
		private function onCreate(evt:MouseEvent):void {
			if(StringUtil.trim(this.contactView.contact.text)!="" &&
				StringUtil.trim(this.contactView.phone.text)!="" &&
				StringUtil.trim(this.contactView.mail.text)!="") {
				
				var service:MainService = new MainService();
				var con:Contact = new Contact();
				con.name = StringUtil.trim(this.contactView.contact.text);
				con.phone = StringUtil.trim(this.contactView.phone.text);
				con.mail = StringUtil.trim(this.contactView.mail.text);
				
				service.addEventListener(ResultEvent.RESULT,contactAdded);
				service.addContact(con);
				this.contactView.closeWindow();
			}else {
				Alert.show("不能有空字段");
			}
		}
		
		private function contactAdded(evt:ResultEvent):void {
			var result:int = evt.result as int;
			if(result>0) {
				Alert.show("添加成功");
				this.sendNotification(EasyMediator.CHANGE_VIEW);
			}else {
				Alert.show("添加失败");
			}
		}
		
		private function onEdit(evt:MouseEvent):void {
			if(StringUtil.trim(this.contactView.contact.text)!="" &&
				StringUtil.trim(this.contactView.phone.text)!="" &&
				StringUtil.trim(this.contactView.mail.text)!="") {
				
				var service:MainService = new MainService();
				var contact:Contact = new Contact();
				contact.id = this.contact.id;
				contact.name = StringUtil.trim(this.contactView.contact.text);
				contact.phone = StringUtil.trim(this.contactView.phone.text);
				contact.mail = StringUtil.trim(this.contactView.mail.text);
				
				service.addEventListener(ResultEvent.RESULT,contactEdited);
				service.updateContact(contact);
				this.contactView.closeWindow();
			}else {
				Alert.show("不能有空字段");
			}
		}
		
		private function contactEdited(evt:ResultEvent):void {
			var result:int = evt.result as int;
			if(result>0) {
				Alert.show("编辑成功");
				this.sendNotification(EasyMediator.CHANGE_VIEW);
			}else {
				Alert.show("编辑失败");
			}
		}
		
		override public function handleNotification(notification:INotification):void {
			switch(notification.getName()) {
			}
		}
		
		public function get contactView():ContactView {
			return this.viewComponent as ContactView;
		}
	}
}